extends MySprites

@onready var animation_player: AnimatedSprite2D = $Animation

func _init():
	# 在初始化时修改变量的默认值
	health = 100
	attack = 30
	defence = 20
	is_player = true
	physical_location = 1
	
func _ready() -> void:
	animation_player.play("Idle")

#蘑菇怪的大招:对敌方一号位造成300%伤害
func use_ultimate_ability(players: Array, enemies: Array) -> void:
	var self_player = players[physical_location]
	var target_enemy: StaticBody2D = get_one_random_my_sprite(enemies)
	var damage = max(1, self_player.attack - target_enemy.defence) * 3
	target_enemy.health -= damage
	animation_player.play("Attack")
	await animation_player.animation_finished
	target_enemy.health -= damage
	if target_enemy.health <= 0:
		target_enemy.health = 0
		target_enemy.my_sprite_killed()
		await target_enemy.animation_player.animation_finished
	animation_player.play("Idle")
	if is_player:
		Events.signal_enemy_turn.emit()
	else:
		Events.signal_player_turn.emit()
		
#蘑菇怪的普通技能:对敌方一号位造成100%伤害
func use_basic_ability(players: Array, enemies: Array) -> void:
	var self_player = players[physical_location]
	var target_enemy: StaticBody2D = get_one_random_my_sprite(enemies)
	var damage = max(1, self_player.attack - target_enemy.defence) * 1
	animation_player.play("Attack")
	await animation_player.animation_finished
	target_enemy.health -= damage
	if target_enemy.health <= 0:
		target_enemy.health = 0
		target_enemy.my_sprite_killed()
		await target_enemy.animation_player.animation_finished
	animation_player.play("Idle")
	if is_player:
		Events.signal_enemy_turn.emit()
	else:
		Events.signal_player_turn.emit()
	
func my_sprite_killed() -> void:
	# 播放死亡动画
	animation_player.play("Death")
	await animation_player.animation_finished
	visible = false

